/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


#include "homepage.h"
#include <appcommon.h>
#include <servicemanager.h>
#include <QtGui/QApplication>
#include <QDeclarativeEngine>
#include <appcommon.h>
#include <servicemanager.h>
#include <QTimer>
#include <QGraphicsObject>
#include <QDeclarativeItem>
#include <QDir>
#include <QDesktopServices>

QDeclarativeListProperty<ImageData> HomePage::imageDatas(){
    return QDeclarativeListProperty<ImageData>(this, &_imageDatas, &imageDatasPropAppend,
            &imageDatasPropCount, &imageDatasPropAt, &imageDatasPropClear);
}

QDeclarativeListProperty<ImageData> HomePage::vedioDatas(){
    return QDeclarativeListProperty<ImageData>(this, &_vedioDatas, &imageDatasPropAppend,
            &imageDatasPropCount, &imageDatasPropAt, &imageDatasPropClear);
}

QDeclarativeListProperty<ImageData> HomePage::musicDatas(){
    return QDeclarativeListProperty<ImageData>(this, &_musicDatas, &imageDatasPropAppend,
            &imageDatasPropCount, &imageDatasPropAt, &imageDatasPropClear);
}

HomePage::HomePage(QObject *parent) : Page(parent) {
    qmlRegisterType<ImageData>();
    mFilmModel = NULL;
    mMusicModel = NULL;
    curCategory = "imageFilm";
    effect = new QGraphicsBlurEffect(this);
    effect->setBlurHints(QGraphicsBlurEffect::PerformanceHint);
    //effect->setBlurRadius(0.0);
}

int HomePage::init() {
    mHomePageView = qobject_cast<HomePageView *>(this->mPageView);
    if(!mHomePageView) {
        return -1;
    }

    getItemData("imageFilm");
    return 0;
}

int HomePage::uninit() {
    if(_imageDatas.empty() == false) {
        foreach(ImageData * i, _imageDatas) {
            delete i;
            i = NULL;
        }
        _imageDatas.clear();
    }

    if(_vedioDatas.empty() == false) {
        foreach(ImageData * i, _vedioDatas) {
            delete i;
            i = NULL;
        }
        _vedioDatas.clear();
    }

    if(_musicDatas.empty() == false) {
        foreach(ImageData * i, _musicDatas) {
            delete i;
            i = NULL;
        }
        _musicDatas.clear();
    }


    return 0;
}

int HomePage::beforeShow() {
    this->mHomePageView->showViewUseQml(this);
    return 0;
}

void HomePage::onSubIconClick(int index) {
    APPINFO << index;
    APPINFO << curCategory;

    QHash<QString, QString> *args = new QHash<QString, QString>();
    if(curCategory == "imageFilm") {
        args->insert("bill_image", mFilmModel->value("bill_image",index).toString());
        args->insert("detail_image", mFilmModel->value("detail_image",index).toString());
        args->insert("file_url", mFilmModel->value("file_url", index).toString());
        SERVICE(RouterService)->redirectTo("FilmDetailPage", args);
    } else if(curCategory == "imageMusic") {
        args->insert("bill_image", mMusicModel->value("bill_image",index).toString());
        args->insert("detail_image", mMusicModel->value("detail_image",index).toString());
        args->insert("music_list_id",mMusicModel->value("id",index).toString());
        SERVICE(RouterService)->redirectTo("MusicPlayingPage", args);
    }

}

int HomePage::afterShow() {
    return 0;
}

int HomePage::onClick(int key) {
    APPINFO << key;
}

int HomePage::onIconClick(QString icon) {
    if(icon == QString("imageMusic")) {
        QHash<QString, QString> *args = new QHash<QString, QString>();
        args->insert("film_category_id", QString("4"));
        SERVICE(RouterService)->redirectTo("MusicPage", args);
    } else if(icon == QString("imageFilm") ) {
        QHash<QString, QString> *args = new QHash<QString, QString>();
        args->insert("film_category_id", QString("4"));
        SERVICE(RouterService)->redirectTo("FilmPage", args);
    } else if(icon == QString("imagePicture") ) {
        SERVICE(RouterService)->redirectTo("PhotoRiverPage");
    } else if(icon == QString("qtQuit")) {
        qApp->quit();
    }
}

int HomePage::getItemData(QString icon) {
    //clear
    if(_imageDatas.empty() == false) {
        foreach(ImageData * i, _imageDatas) {
            delete i;
            i = NULL;
        }
        _imageDatas.clear();
    }

    if(_vedioDatas.empty() == false) {
        foreach(ImageData * i, _vedioDatas) {
            delete i;
            i = NULL;
        }
        _vedioDatas.clear();
    }

    if(_musicDatas.empty() == false) {
        foreach(ImageData * i, _musicDatas) {
            delete i;
            i = NULL;
        }
        _musicDatas.clear();
    }

    QString indexName ="";
    QString indexImage = "";


    //vedio
    mFilmModel = new DbModel(QString("select * from films where film_category_id = %1").arg(5));
    if(mFilmModel->count() <= 0) {
        return -1;
    }

    for(int i=0; i< mFilmModel->count(); i++) {
        indexName = mFilmModel->value("name",i).toString();
        indexImage =QFileInfo("./cn/" + mFilmModel->value("index_image",i).toString()).absoluteFilePath();
        _vedioDatas.append(new ImageData(indexName,indexImage));
    }

    //music
    mMusicModel = new DbModel(QString("select * from musics_list where music_category_id = %1").arg(4));

    if(mMusicModel->count() <= 0) {
        return -1;
    }

    for(int i=0; i< mMusicModel->count(); i++) {
        indexName = mMusicModel->value("name",i).toString();
        indexImage =QFileInfo("./cn/" + mMusicModel->value("index_image",i).toString()).absoluteFilePath();
        _musicDatas.append(new ImageData(indexName,indexImage));
    }
    //image
    APPINFO << SERVICE(PicCovertService)->getList()->count();
    for(int i = 0; i < SERVICE(PicCovertService)->getList()->count(); i++)
    {
        indexName = "";
        indexImage = SERVICE(PicCovertService)->getList()->at(i);

        APPINFO << indexImage;
        _imageDatas.append(new ImageData(indexName, indexImage));
    }

}

int HomePage::onIconEnter(QString icon) {
    curCategory = icon;
}

void HomePage::setBlurEffect(const QString& objectName, bool enable)
{

    QDeclarativeItem *item = this->mHomePageView->getQmlView()->rootObject()->findChild<QDeclarativeItem*>(objectName);
    if(item && !item->graphicsEffect()) {
        item->setGraphicsEffect(effect);
    }

    effect->setEnabled(enable);
}

int HomePage::onTouch(const QString &type, const QHash<QString, QString> *args) {
    return 0;
}

void HomePage::onClickButton(const QString &buttonName) {
}

void HomePage::changeToFilm() {
}


void HomePage::changeLanguage() {
}
